package com.wjjun.base.ui.base;

import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.qingmi.farmer.R;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;
import com.wjjun.presenter.ViewGroupPresenter;
import com.wjjun.viewmodel.CommListViewModel;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 列表activity
 * Created by 67535 on 2018/5/7.
 */
public abstract class ListActivity<VM extends ListViewModel, VDB extends ViewDataBinding> extends BaseActivity<VM, VDB> {

    public int PAGE_SIZE = 20;

    protected ObservableList data = new ObservableArrayList<>();

    protected CommListViewModel commListViewModel;

    public ViewGroupPresenter viewGroupPresenter;

    protected RefreshLayout mRefreshLayout;

    protected boolean dataChange = false;

    @Override
    protected void initContent() {
        super.initContent();
        initViewGroupPresenter();
        initListViewModel();
        viewModel.setCommList(commListViewModel);
        mRefreshLayout = viewDataBinding.getRoot().findViewById(R.id.refreshLayout);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (dataChange) {
            dataChange = false;
            autoRefresh();
        }
    }

    private void initViewGroupPresenter() {
        viewGroupPresenter = new ViewGroupPresenter() {
            @Override
            public void onItemClick(View view, BaseViewModel viewModel) {
                onListItemClick(view, viewModel);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (refreshLayout != null) {
                    mRefreshLayout = refreshLayout;
                }
                refreshData(false);
            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if (refreshLayout != null) {
                    mRefreshLayout = refreshLayout;
                }
                loadData();
            }

            @Override
            public void onClick(View view) {
                onListClick(view);
            }
        };
    }

    protected void initListViewModel() {
        commListViewModel = new CommListViewModel(data, new OnItemBind() {

            @Override
            public void onItemBind(ItemBinding itemBinding, int position, Object item) {
                onListItemBind(itemBinding, position, item);
            }
        });
        commListViewModel.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                Object item = null;
                if (data != null && data.size() > position) {
                    item = data.get(position);
                }
                return getItemSpanSize(position,  item, commListViewModel.getSpanCount());
            }
        });
    }

    /**
     * 用于网格布局时,指定每个item的宽度,
     * 于recycleView的spanCount对应, 当返回panCount时, 即该item占用一整行
     * @param position
     * @param item
     * @return
     */
    public int getItemSpanSize(int position, Object item, int maxSpan) {
        return 1;
    }

    /**
     * item与布局绑定
     *
     * @param itemBinding
     * @param position
     * @param itemViewModel
     */
    public abstract void onListItemBind(ItemBinding itemBinding, int position, Object itemViewModel);

    protected void onListItemClick(View view, BaseViewModel itemViewModel) {

    }

    protected void initData() {
        refreshData(true);
    }

    public final void refreshData(){
        refreshData(false);
    }

    public void refreshData( boolean init) {
        if (commListViewModel.isErrorPage()) {
            commListViewModel.showLoading();
        }
    }


    public void loadData() {
        finishLoadMore(false);
    }

    protected void onListClick(View view) {

    }

    public void addData(List<? extends BaseViewModel> itemViewModels) {
        addData(itemViewModels, PAGE_SIZE);
    }

    @SuppressWarnings("unchecked")
    public void addData(List<? extends BaseViewModel> itemViewModels, int pageSize) {
        if (itemViewModels == null || itemViewModels.isEmpty()) {
            finishLoadMore(true);
        } else {
            data.addAll(itemViewModels);
            if (itemViewModels.size() < pageSize) {
                finishLoadMore(true);
            } else {
                finishLoadMore(false);
            }
        }

    }

    @SuppressWarnings("unchecked")
    public void resetData(List<? extends BaseViewModel> itemViewModels) {
        data.clear();
        if (itemViewModels == null || itemViewModels.isEmpty()) {
            commListViewModel.showEmpty();
        } else {
            data.addAll(itemViewModels);
            commListViewModel.showSuccess();
        }
        finisRefresh();
    }

    @SuppressWarnings("unchecked")
    public void resetData(BaseViewModel header, List<? extends BaseViewModel> itemViewModels) {
        data.clear();
        data.add(header);
        if (itemViewModels == null || itemViewModels.isEmpty()) {
            commListViewModel.showEmpty();
        } else {
            data.addAll(itemViewModels);
            commListViewModel.showSuccess();
        }
        finisRefresh();
    }

    @SuppressWarnings("unchecked")
    public void resetData(List<? extends BaseViewModel> itemViewModels, BaseViewModel footer) {
        data.clear();
        if (itemViewModels == null || itemViewModels.isEmpty()) {
            commListViewModel.showEmpty();
        } else {
            data.addAll(itemViewModels);
            commListViewModel.showSuccess();
        }
        data.add(footer);
        finisRefresh();
    }

    @SuppressWarnings("unchecked")
    public void addItem(BaseViewModel itemViewModel) {
        data.add(itemViewModel);
    }

    @SuppressWarnings("unchecked")
    public void addItem(int index, BaseViewModel itemViewModel) {
        data.add(index, itemViewModel);
    }

    public void removeItem(BaseViewModel itemViewModel) {
        data.remove(itemViewModel);
    }

    public void setError(String msg) {
        finisRefresh();
        commListViewModel.setError(msg);
        commListViewModel.showError();
    }

    public void finisRefresh() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh(50);
            mRefreshLayout.setNoMoreData(false);
        }
    }

    public void showLogin(){
        commListViewModel.showLogin();
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh();
            mRefreshLayout.setNoMoreData(false);
            mRefreshLayout.finishLoadMore(true);
        }
    }

    public void notifyDataChange() {
        dataChange = true;
    }

    public void autoRefresh() {
        if (commListViewModel.isErrorPage()) {
            commListViewModel.showLoading();
        }
        if (mRefreshLayout != null) {
            mRefreshLayout.autoRefresh(10, 100, 1);
        }
    }

    public void finishLoadMore(boolean noMoreData) {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadMore(50, true, noMoreData);
        }
    }

    public void finishLoadMoreError() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadMore(false);
        }
    }
}
